package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements Operation{
    @Override
    public void work(BookList bookList){
        Scanner scanner = new Scanner(System.in);
        System.out.println("添加图书");
        System.out.println("请输入书名：");
        String name = scanner.nextLine();
        System.out.println("请输入作者名字：");
        String author = scanner.nextLine();
        System.out.println("请输入价格：");
        int price = scanner.nextInt();
        System.out.println("请输入书本类型：");
        String type = scanner.next();
        Book book = new Book(name,author,price,type);
        int currentSize = bookList.getUsedsize();
        for (int i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBook(i);
            if(tmp.getName().equals(name)){
                System.out.println("该书已存在，不能添加");
                return;
            }
        }
        bookList.setBook(book);
        bookList.setUsedsize(currentSize+1);
    }
}
